var app = getApp()
Page({
  data: {
    commentId: '',
    grade: 0,
    hasPhoneNum:false,
    remark: '',
    heartList: [{
      isSelected: false
    }, {
      isSelected: false
    }, {
      isSelected: false
    }, {
      isSelected: false
    }, {
      isSelected: false
    }],
    isSaved: false,
    isAuditing: false,
    isConfigQuestion:false,
    gradeTexts: ['您的评价，让我们做得更好', '不满意！态度极差！', '一般！服务态度不好！', '不错！感觉还可以！', '满意！整体感觉都挺好！', '超赞！感觉棒极了！']
  },
  getSysConfig() {
    var that = this
    return new Promise((resolve, reject) => {
      app.post(app.Apis.GET_CONFIGS, {
        groupId: 'XCX_MEMBER',
        belongToId: 'xcx_carelinker_consult'
      }, result => {
        console.log(result)
        if (result.success) {
          let configs = {}
          result.dataList.forEach(item => {
            configs[item.key] = item.value
          })
          let isAuditing = configs['XCX_CONSULT_AUDITING']
          console.log(isAuditing)
          if (isAuditing == app.ver){
            app.belongToId = '9990000'
          }
          resolve(isAuditing)
          that.getConfigQuestion()
        }
      })
    })
  },
  // 提交
  postDetail() {
    if(!app.hasPhoneNum){
      return
    }
    if (!this.data.grade) {
      wx.showModal({
        title: '提示',
        content: '请选择评分',
        showCancel: false
      })
      return
    }
    wx.showLoading({
      title: '提交中...',
      mask: true
    })
    if (this.data.isAuditing) {
      setTimeout(() => {
        wx.showToast({
          title: '提交成功',
        })
        this.setData({
          isSaved: true
        })
      }, 1000)
      return
    }
    if (this.data.commentId) {
      let data = {
        commentId: this.data.commentId,
        remark: this.data.remark,
        grade: this.data.grade
      }
      app.post(app.Apis.POST_COMMENTS_DETAIL, data, result => {
        if (result.success) {
          wx.hideLoading()
          wx.showToast({
            title: '提交成功'
          })
          this.setData({
            isSaved: true
          })
        }
      })
    }
  },
  getDetail() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    let data = {
      'commentId': this.data.commentId
    }
    app.post(app.Apis.GET_COMMENTS_DETAIL, data, result => {
      console.log(result)
      if (result.success) {
        let index = result.data.grade || 1
        let heartList = this.data.heartList.map((item, i) => {
          item.isSelected = i < (index - 1)
          return item
        })
        this.setData({
          grade: result.data.grade,
          remark: result.data.remark,
          heartList,
          isSaved: result.data.grade > 0
        })
      }
      wx.hideLoading()
    })
  },
  handleOnSelect(e) {
    if (!this.data.isSaved) {
      let index = parseInt(e.currentTarget.id)
      let heartList = this.data.heartList.map((item, i) => {
        item.isSelected = i <= index
        return item
      })
      this.setData({
        grade: index + 1,
        heartList
      })
    }
  },
  hanldeOnInput(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  handleSubmit() {
    if (!this.data.isSaved) {
      this.postDetail()
    } else {
      wx.showModal({
        title: '提示',
        content: '你已经提交过评价，请勿重复操作！',
        showCancel: false
      })
    }
  },
  onLoad: function (options) {
    app.loginUser()
    var that = this
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        console.log(res)
        app.hasPhoneNum = true
        app.userId = res.data.userId
        app.openid = res.data.openid
        that.setData({
          hasPhoneNum: true
        })
      },
      fail: function (res) {
        console.log(res)

      }
    })
    this.getSysConfig().then(res => {
      let isAuditing = res === app.ver
      let commentId = options.commentId || ''
      console.log(commentId)
      this.setData({
        commentId,
        isAuditing
      })
      if (commentId) {
        this.getDetail()
      }
    })
  },
  onShow: function () {
  },
  // 点击药咨询
  clickYZXaction(){
    wx.navigateTo({
      url: '/pages/consultationHome/index',
    })
  },

  getPhoneNumber(e){
    console.log(e)
    var that = this
    app.getPhoneNum(e, result => {
      that.setData({
        hasPhoneNum: true
      })
      that.postDetail()
    })

  },
  getConfigQuestion(){
    var that = this
    app.getSysConfig('XCX_CONSULT', result => {
      console.log(result)
      let configs = {}
      result.dataList.forEach(item => {
        configs[item.key] = item.value
      })
      let isConfigQuestion = configs["OPEN_XCX_CONSULT_QUESTION"]
      let isConfigDrug = configs["OPEN_XCX_CONSULT_DRUG"]
      app.isConfigQuestion = isConfigQuestion == 'true'
      app.isConfigDrug = isConfigDrug == 'true'
      that.setData({
        isConfigQuestion: app.isConfigQuestion
      })
    })
  }
})